package algorithm.nlp.corenlp;

import algorithm.config.HumanLanguage;
import algorithm.config.ParserType;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * Created by common on 2017/4/8.
 */
public class CoreNLPFactory {
    static final Logger logger = LoggerFactory.getLogger(CoreNLPFactory.class);


    private StanfordCoreNLP pipeline;


    public CoreNLPFactory(){
        logger.info("[CoreNLPFactory] Factory Created!");
    }


    public StanfordCoreNLP getPipeline(HumanLanguage language){
        switch (language){
            case ENGLISH:
                this.pipeline = getPipelineOfEnglish();
                break;
            case CHINESE:
                this.pipeline = getPipelineOfChinese();
                break;
            default:
                this.pipeline = getPipelineOfEnglish();
        }

        return this.pipeline;
    }

    public StanfordCoreNLP getPipelineByParser(ParserType parserType){
        switch (parserType) {
            case DEPENDENCY:
                this.pipeline = getPipelineOfDependency();
                break;
            default:
                this.pipeline = getPipelineOfChinese();

        }
        return this.pipeline;
    }


    private StanfordCoreNLP getPipelineOfDependency(){
        Properties props = new Properties();
        //props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,depparse");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        return pipeline;
    }

    private StanfordCoreNLP getPipelineOfEnglish(){
        Properties props = new Properties();
        // coref 耗内存，dcoref 省内存
        //props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
        props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,mention,coref");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        return pipeline;
    }


    private StanfordCoreNLP getPipelineOfChinese(){
        String props="StanfordCoreNLP-chinese.properties";
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        return pipeline;
    }



}
